layui.define(["form", "jquery", 'admin', 'layer'], function (exports) {
    var form = layui.form
        , $ = layui.jquery
        , admin = layui.admin
        , layer = layui.layer
        , linkselect3 = {
            option:{}
            , claretwos: function (optionm) {
                $("select[name=" + optionm.elem + '-' + optionm.two + "]").html('<option value="0">请选择' + optionm.twotitle + '</option>');
                $("select[name=" + optionm.elem + '-' + optionm.two + "]").attr("disabled", "disabled");
                form.render();
                linkselect3.clarethrees(optionm);
            }

            , clarethrees: function (optionm) {
                $("select[name=" + optionm.elem + '-' + optionm.three + "]").html('<option value="0">请选择' + optionm.threetitle + '</option>');
                $("select[name=" + optionm.elem + '-' + optionm.three + "]").attr("disabled", "disabled");
                form.render();
            }
            , ones: function (data, optionm) {
                var proHtml = '', that = this;
                
                for (var i = 0; i < data.length; i++) {
                    proHtml += '<option value="' + data[i].code + '">' + data[i].name + '</option>';
                }

                $("select[name=" + optionm.elem + '-' + optionm.one + "]").append(proHtml);
                form.render();
                form.on("select(" + optionm.elem + '-' + optionm.one + ")", function (proData) {
                    var optionm=that.option;

                    linkselect3.clarethrees(optionm);
                    var value = proData.value;

                    if (value > 0) {
                        that.twos(data[$(this).index() - 1].childs, optionm);
                    } else {
                        linkselect3.claretwos(optionm);
                    }
                });
            }
            , twos: function (twos, optionm) {
                var twoHtml = '<option value="0">请选择' + optionm.twotitle + '</option>', that = this;
                for (var i = 0; i < twos.length; i++) {
                    twoHtml += '<option value="' + twos[i].code + '">' + twos[i].name + '</option>';
                }
                $("select[name=" + optionm.elem + '-' + optionm.two + "]").html(twoHtml).removeAttr("disabled");
                form.render();
                form.on('select(' + optionm.elem + '-' + optionm.two + ')', function (twoData) {
                    var value = twoData.value;
                    if (value > 0) {
                        that.threes(twos[$(this).index() - 1].childs, that.option);
                    } else {
                        linkselect3.clarethrees(that.option);
                    }
                });
            }
            , threes: function (threes, optionm) {
                var threeHtml = '<option value="0">请选择' + optionm.threetitle + '</option>';
                for (var i = 0; i < threes.length; i++) {
                    threeHtml += '<option value="' + threes[i].code + '">' + threes[i].name + '</option>';
                }
                $("select[name=" + optionm.elem + '-' + optionm.three + "]").html(threeHtml).removeAttr("disabled");
                form.render();
            }
            , render: function (optionm) {
                linkselect3.option=optionm;
                var dst = $("#" + optionm.elem);
                var dstHtml = '';
                if (optionm.label != "") {
                    dstHtml += ' <label class="layui-form-label">' + optionm.label + '</label>'
                }
                dstHtml += '    <div class="layui-input-block">'
                dstHtml += '        <div class="layui-input-inline">'
                dstHtml += '<select name="' + optionm.elem + '-' + optionm.one + '" lay-filter="' + optionm.elem + '-' + optionm.one + '" class="province">'
                dstHtml += '    <option value="0">请选择' + optionm.onetitle + '</option>'
                dstHtml += '</select>'
                dstHtml += '</div>'
                dstHtml += '<div class="layui-input-inline">'
                dstHtml += '<select name="' + optionm.elem + '-' + optionm.two + '" lay-filter="' + optionm.elem + '-' + optionm.two + '" disabled>'
                dstHtml += '    <option value="0">请选择' + optionm.twotitle + '</option>'
                dstHtml += '</select>'
                dstHtml += '</div>'
                dstHtml += '<div class="layui-input-inline">'
                dstHtml += '<select name="' + optionm.elem + '-' + optionm.three + '" lay-filter="' + optionm.elem + '-' + optionm.three + '" disabled>'
                dstHtml += '    <option value="0">请选择' + optionm.threetitle + '</option>'
                dstHtml += '</select>'
                dstHtml += '</div>'

                dstHtml += '</div>';
                dst.html(dstHtml);
                admin.req({
                    url: optionm.url
                    , done: function (res) {
                        linkselect3.ones(res.data, optionm)
                    }
                })
            }
        }

    exports("linkselect3", linkselect3);
});